<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
<!--      <el-form-item label="单位" prop="unitId">-->
<!--        <el-select v-model="queryParams.unitId" placeholder="请选择单位" style="width: 200px">-->
<!--          <el-option-->
<!--            v-for="item in unitList"-->
<!--            :key="item.id"-->
<!--            :label="item.unitName"-->
<!--            :value="item.id"-->
<!--          />-->
<!--        </el-select>-->
<!--      </el-form-item>-->
      <el-form-item label="建筑物" prop="bgId">
        <el-select v-model="queryParams.bgId" placeholder="请选择结构类型" style="width: 200px"
                   @keyup.enter.native="handleQuery">
          <el-option
            v-for="item in bgList"
            :key="item.id"
            :label="item.bdName"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="联网楼层" prop="floorId">
        <el-input
          v-model="queryParams.floorId"
          placeholder="请输入联网楼层"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="楼层名称" prop="floorName">
        <el-input
          v-model="queryParams.floorName"
          placeholder="请输入楼层名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:floor:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:floor:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:floor:remove']"
          v-hasRole="['admin']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:floor:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="floorList" @selection-change="handleSelectionChange"
              :row-class-name="tableRowClassName">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="建筑物" align="center" prop="building.bdName" width="300"/>
      <el-table-column label="楼层名称" align="center" prop="floorName" />
      <el-table-column label="楼层索引" align="center" prop="floorIndex" />
      <el-table-column label="楼层面积" align="center" prop="floorArea" />
      <el-table-column label="房间数量" align="center" prop="floorRoomCount" />
<!--      <el-table-column label="楼层高度" align="center" prop="floorHeight" />-->
      <el-table-column label="是否是避难层" align="center" prop="floorIsRefuge" >
        <template slot-scope="scope">
          {{scope.row.floorIsRefuge === 0 ? '是' : '否'}}
        </template>
      </el-table-column>
<!--      <el-table-column label="自定义数据" align="center" prop="extraData" />-->
      <el-table-column label="推送状态" align="center" prop="pushStatus" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.push_status" :value="scope.row.pushStatus"/>
        </template>
      </el-table-column>
<!--      <el-table-column label="创建用户ID" align="center" prop="createUserId" />-->
<!--      <el-table-column label="备注" align="center" prop="remark" />-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button v-if="scope.row.pushStatus ==='2'"
                     size="mini"
                     type="text"
                     icon="el-icon-bottom"
                     @click="handleCancel(scope.row)"
                     v-hasPermi="['system:unit:edit']"
                     v-hasRole="['admin']"
          >取消推送
          </el-button>
          <el-button v-if="scope.row.pushStatus !=='2'"
            size="mini"
            type="text"
            icon="el-icon-top"
            @click="handleSort(scope.row)"
            v-hasPermi="['system:unit:edit']"
          >推送
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:floor:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:floor:remove']"
            style="color: #ff0000;border: none"
            v-hasRole="['admin']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改楼层信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="60%" append-to-body :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="150px" inline>
        <el-form-item label="建筑物" prop="bgId">
          <el-select v-model="form.bgId" placeholder="请选择结构类型" style="width: 200px">
            <el-option
              v-for="item in bgList"
              :key="item.id"
              :label="item.bdName"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
<!--        <el-form-item label="联网楼层" prop="floorId">-->
<!--          <el-input v-model="form.floorId" placeholder="请输入联网楼层ID" />-->
<!--        </el-form-item>-->
        <el-form-item label="楼层名称" prop="floorName">
          <el-input v-model="form.floorName" placeholder="请输入楼层名称,最大长度 64" />
        </el-form-item>
        <el-form-item label="楼层索引" prop="floorIndex">
          <el-input v-model="form.floorIndex" placeholder="请输入楼层索引,楼层数值" />
        </el-form-item>
        <el-form-item label="楼层面积" prop="floorArea">
          <el-input v-model="form.floorArea" placeholder="请输入楼层面积,单位：m2" />
        </el-form-item>
        <el-form-item label="房间数量" prop="floorRoomCount">
          <el-input v-model="form.floorRoomCount" placeholder="请输入房间数量" />
        </el-form-item>
        <el-form-item label="楼层高度" prop="floorHeight">
          <el-input v-model="form.floorHeight" placeholder="请输入楼层高度,单位：m，精确到小数点后2位" />
        </el-form-item>
        <el-form-item label="是否是避难层" prop="floorIsRefuge">
          <el-select v-model="form.floorIsRefuge" placeholder="请选择是否是避难层" style="width: 200px">
            <el-option label="是" value="0"/>
            <el-option label="不是" value="1"/>
          </el-select>
        </el-form-item>
<!--        <el-form-item label="自定义数据" prop="extraData">-->
<!--          <el-input v-model="form.extraData" placeholder="请输入自定义数据" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="创建用户" prop="createUserId">-->
<!--          <el-input v-model="form.createUserId" placeholder="请输入创建用户ID" />-->
<!--        </el-form-item>-->
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
<!--        <el-form-item label="删除标识" prop="delFlag">-->
<!--          <el-input v-model="form.delFlag" placeholder="请输入删除标识" />-->
<!--        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listFloor, getFloor, delFloor, addFloor, updateFloor, floorPush, delFloorId} from "@/api/system/floor"
import {listUnit} from "@/api/system/unit";
import {listBuilding} from "@/api/system/building";

export default {
  name: "Floor",
  dicts: ['push_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 楼层信息表格数据
      floorList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        bgId: null,
        floorId: null,
        floorName: null,
        floorIndex: null,
        floorArea: null,
        floorRoomCount: null,
        floorHeight: null,
        floorIsRefuge: null,
        extraData: null,
        pushStatus: null,
        createUserId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        bgId: [
          { required: true, message: "建筑物ID不能为空", trigger: "blur" }
        ],
        floorName: [
          { required: true, message: "楼层名称,最大长度 64不能为空", trigger: "blur" }
        ],
        floorIndex: [
          { required: true, message: "楼层索引,楼层数值不能为空", trigger: "blur" }
        ],
        floorArea: [
          { required: true, message: "楼层面积,单位：m2不能为空", trigger: "blur" }
        ],
      },
      unitList:[],//单位list
      bgList:[],//单位list
    }
  },
  created() {
    this.getList()
    this.getUnit()
    this.getBg()
  },
  methods: {
    /** 查询楼层信息列表 */
    getList() {
      this.loading = true
      listFloor(this.queryParams).then(response => {
        this.floorList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        bgId: null,
        floorId: null,
        floorName: null,
        floorIndex: null,
        floorArea: null,
        floorRoomCount: null,
        floorHeight: null,
        floorIsRefuge: null,
        extraData: null,
        pushStatus: null,
        createUserId: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        delFlag: null
      }
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = "添加楼层信息"
    },
    /** 推送按钮操作 */
    handleSort(row) {
      const id = row.id
      floorPush(id).then(response => {
        this.$modal.msgSuccess("推送成功！")
        this.getList()
      })
    },
    /** 取消推送按钮操作 */
    handleCancel(row) {
      const floorId = row.id
      delFloorId(floorId).then(response => {
        this.$modal.msgSuccess("推送成功！")
        this.getList()
      })
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getFloor(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = "修改楼层信息"
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateFloor(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            addFloor(this.form).then(response => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除楼层名称为"' + row.floorName + '"的数据项？').then(function() {
        return delFloor(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/floor/export', {
        ...this.queryParams
      }, `floor_${new Date().getTime()}.xlsx`)
    },
    /** 查询单位 */
    getUnit() {
      listUnit({
        pageNum: 1,
        pageSize: 999999,
      }).then(response => {
        this.unitList = response.rows
      })
    },
    /** 查询建筑物 */
    getBg() {
      listBuilding({
        pageNum: 1,
        pageSize: 999999,
      }).then(response => {
        this.bgList = response.rows
      })
    },
    // 是否推送表格颜色
    tableRowClassName({row}) {
      if (row.pushStatus === '1') {
        return 'warning-row';
      } else if (row.pushStatus === '2') {
        return 'success-row';
      }
      return '';
    },
  }
}
</script>
<style>
.el-table .warning-row {
  background: oldlace;
}

.el-table .success-row {
  background: #f0f9eb;
}
</style>
